package com.example.mobile2021606128wje0615;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class StudentRVAdapter  extends RecyclerView.Adapter<StudentRVAdapter.ViewHolder>{
    private List<StudentScore> list;
    public StudentRVAdapter(List<StudentScore> list){
        this.list=list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取列表布局文件，创建ViewHolder对象
        View view= LayoutInflater.from( parent.getContext() ).inflate( R.layout.item_rv_main_student,parent,false );
        return new ViewHolder( view );
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //获取数据内容，然后赋值给控件
        StudentScore studentScore=list.get( position );
        holder.tvItemNum.setText( String.valueOf( studentScore.get_id() ) );
        holder.tvItemName.setText(studentScore.getName());
        holder.tvItemAge.setText( String.valueOf( studentScore.getAge() ) );
        holder.tvItemScore.setText( String.valueOf( studentScore.getScore() ) );



    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //定义viewHolder静态内部类，继承RecycView.ViewHolder，用于描述列表项文件的控件
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvItemNum,tvItemName,tvItemAge,tvItemScore;



        public ViewHolder(@NonNull View itemView) {
            super( itemView );
            tvItemNum=itemView.findViewById( R.id.tvItemNum );
            tvItemName=itemView.findViewById( R.id.tvItemName);
            tvItemAge=itemView.findViewById( R.id.tvItemAge);
            tvItemScore=itemView.findViewById( R.id.tvItemScore);


        }
    }
}
